#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
#include <unistd.h>

#define BUFFER_SIZE 1024

// AVRCP Command IDs
#define AVRCP_PLAY 0x44
#define AVRCP_PAUSE 0x45
#define AVRCP_STOP 0x46
#define AVRCP_NEXT 0x47
#define AVRCP_PREVIOUS 0x48

// 发送 AVRCP 命令
void send_avrcp_command(int socket, uint8_t command) {
    uint8_t buffer[BUFFER_SIZE];
    memset(buffer, 0, sizeof(buffer));

    // 构建 AVRCP 命令包
    buffer[0] = 0x0; // 版本
    buffer[1] = 0x0; // 事务 ID
    buffer[2] = 0x00; // PDU ID
    buffer[3] = 0x00; // 目标地址
    buffer[4] = 0x00; // 目标地址
    buffer[5] = command; // AVRCP 命令

    // 发送命令
    write(socket, buffer, sizeof(buffer));
}

int main() {
    // 初始化蓝牙套接字
    int sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
    if (sock < 0) {
        perror("Failed to create socket");
        return 1;
    }

    // 绑定到蓝牙地址
    struct sockaddr_l2 addr = {0};
    addr.l2_family = AF_BLUETOOTH;
    addr.l2_psm = htobs(0x1001); // PSM (Protocol/Service Multiplexer)

    // 绑定套接字
    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("Failed to bind socket");
        close(sock);
        return 1;
    }

    // 监听连接
    listen(sock, 1);
    printf("Waiting for connections...\n");

    // 接受连接
    struct sockaddr_l2 client;
    socklen_t client_len = sizeof(client);
    int client_sock = accept(sock, (struct sockaddr *)&client, &client_len);
    if (client_sock < 0) {
        perror("Failed to accept connection");
        close(sock);
        return 1;
    }

    printf("Connection accepted\n");

    // 发送 AVRCP 命令示例
    send_avrcp_command(client_sock, AVRCP_PLAY); // 播放
    sleep(1);
    send_avrcp_command(client_sock, AVRCP_PAUSE); // 暂停
    sleep(1);
    send_avrcp_command(client_sock, AVRCP_NEXT); // 下一首
    sleep(1);
    send_avrcp_command(client_sock, AVRCP_PREVIOUS); // 上一首

    // 关闭套接字
    close(client_sock);
    close(sock);
    return 0;
}

/*

此函数是使用avrcp进行控制 音乐的播放功能
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/sco.h>
#include <bluetooth/l2cap.h>
#include <unistd.h>

#define BUFFER_SIZE 1024

// HFP Command IDs
#define HFP_ANSWER_CALL 0x01
#define HFP_HANGUP_CALL 0x02

// 发送 HFP 命令
void send_hfp_command(int socket, uint8_t command) {
    uint8_t buffer[BUFFER_SIZE];
    memset(buffer, 0, sizeof(buffer));

    // 构建 HFP 命令包
    buffer[0] = command; // HFP 命令

    // 发送命令
    write(socket, buffer, sizeof(buffer));
}

int main() {
    // 初始化蓝牙套接字
    int sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
    if (sock < 0) {
        perror("Failed to create socket");
        return 1;
    }

    // 绑定到蓝牙地址
    struct sockaddr_l2 addr = {0};
    addr.l2_family = AF_BLUETOOTH;
    addr.l2_psm = htobs(0x1001); // PSM (Protocol/Service Multiplexer)

    // 绑定套接字
    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("Failed to bind socket");
        close(sock);
        return 1;
    }

    // 监听连接
    listen(sock, 1);
    printf("Waiting for connections...\n");

    // 接受连接
    struct sockaddr_l2 client;
    socklen_t client_len = sizeof(client);
    int client_sock = accept(sock, (struct sockaddr *)&client, &client_len);
    if (client_sock < 0) {
        perror("Failed to accept connection");
        close(sock);
        return 1;
    }

    printf("Connection accepted\n");

    // 接听电话示例
    send_hfp_command(client_sock, HFP_ANSWER_CALL); // 接听电话
    sleep(1);
    send_hfp_command(client_sock, HFP_HANGUP_CALL); // 挂断电话

    // 关闭套接字
    close(client_sock);
    close(sock);
    return 0;
}

/*
此函数是 接听电话，挂断电话的通信

*/




#include <stdint.h>

// CRC计算函数（示例，具体实现可能不同）
uint8_t calculateCRC(uint8_t *data, uint8_t length) {
    uint8_t crc = 0;
    for (uint8_t i = 0; i < length; i++) {
        crc ^= data[i]; // 简单的异或操作，实际应用中可能使用更复杂的CRC算法
    }
    return crc;
}

// 发送数据函数
void sendData(uint8_t command) {
    uint8_t data[2] = {0xFF, command}; // 起始字节 + 命令字节
    uint8_t crc = calculateCRC(data, 2); // 计算CRC
    uint8_t packet[4] = {0xFF, command, crc, 0xFE}; // 完整数据包

    // 发送数据包
    Bluetooth.write(packet, sizeof(packet));
}



void receiveData() {
    if (Bluetooth.available()) {
        uint8_t startByte = Bluetooth.read();
        if (startByte == 0xFF) {
            uint8_t command = Bluetooth.read();
            uint8_t crc = Bluetooth.read();
            uint8_t endByte = Bluetooth.read();

            // 验证结束字节
            if (endByte == 0xFE) {
                // 计算接收到的数据的CRC
                uint8_t data[2] = {0xFF, command};
                uint8_t calculatedCRC = calculateCRC(data, 2);

                // 验证CRC
                if (calculatedCRC == crc) {
                    // CRC验证成功，处理命令
                    switch (command) {
                        case 0x01:
                            playPreviousTrack();
                            break;
                        case 0x02:
                            playNextTrack();
                            break;
                        case 0x03:
                            answerCall();
                            break;
                    }
                } else {
                    // CRC验证失败，丢弃数据
                    Serial.println("CRC error!");
                }
            }
        }
    }
}